﻿package tech.deepmind.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tech.deepmind.dao.BaseDao;
import tech.deepmind.dao.UserDao;
import tech.deepmind.entity.User;

/**
 * service层捕获异常，进行事务处理
 * 事务处理：调用不同dao的多个方法，必须使用同一个connection（connection作为参数传递）
 * 事务完成之后，需要在service层进行connection的关闭，在dao层关闭（PreparedStatement和ResultSet对象）
 * @author Administrator
 *
 */
@Service
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDao userDao;
	@Override
	public boolean add(User user) {
		return userDao.add(user)>0?true:false;
	}
	@Override
	public User login(String userCode, String userPassword) {
		User user = userDao.getLoginUser(userCode);
		//匹配密码
		if(null != user){
			if(!user.getUserPassword().equals(userPassword))
				user = null;
		}
		return user;
	}
	@Override
	public List<User> getUserList(String queryUserName,int queryUserRole,int currentPageNo, int pageSize) {
		
		return userDao.getUserList(queryUserName, queryUserRole, currentPageNo, pageSize);
	}
	@Override
	public User selectUserCodeExist(String userCode) {
		return userDao.getLoginUser(userCode);
	}
	@Override
	public boolean deleteUserById(Integer delId) {
		return userDao.deleteUserById(delId)>0?true:false;
	}
	@Override
	public User getUserById(String id) {
		return userDao.getUserById(id);
	}
	@Override
	public boolean modify(User user) {
		return userDao.modify(user)>0?true:false;
	}
	@Override
	public boolean updatePwd(int id, String pwd) {
		
		return userDao.updatePwd(id, pwd)>0?true:false;
	}
	@Override
	public int getUserCount(String queryUserName, int queryUserRole) {
		
		return userDao.getUserCount(queryUserName, queryUserRole);
	}
	
}
